// pages/broadcast/broadcast.js
const app = getApp();
Page({
  data: {
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '公告',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333;background-color: #f1f1f1;",
    navs:[
      {title: "首页",class:"", path:"/pages/index/index", icon: "", font: "fa fa-home"},
      {title: "信息",class:"", path:"", icon: "", font: "fa fa-comments-o"},
      {title: "维修",class:"", path:"", icon: "", font: "fa fa-wrench"},
      {title: "我的",class:"co", path:"/pages/mine/mine", icon: "", font: "fa fa-user-circle"},
    ],
    userInfo: {
      nickName: "",
      addr: "",
      phone: "",
      avatar: ""
    },
    notice: "",
    notice_detail: '',
    noticeList:[],
    listMode: false,
    hasChange: false,
    notice_id: 0,
    ready: false
  },
  onLoad: function (options) {
    console.log("onLoad",options);
    if(options.notice_id) {
      this.setData({notice_id:options.notice_id});
    }
  },
  onReady: function () {
  },
  
  onShow: function () {
    let v = this;
    if (app.globalData.token){
      // v.getNotice();
      v.getNoticeDetail();
    } else {
      app.doLogin(v,v.getNotice);
    }
  },
  switchMode(ev) {
    let v = this;
    let listMode = !v.data.listMode;
    if(listMode) {
      v.getNoticeList();
    } else {
      console.log("switchMode", ev);
      let ds = ev.target.dataset;
      if(ds.id) {
        v.setData({notice_id: ds.id});
        v.getNoticeDetail(ds.id);
      }
    }
  },
  getNoticeList(){
    let v = this;
    let url = app.globalData.host + '/api/notice/list';
    let token = app.globalData.token;
    wx.request({ url:url, data:{token: token},success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          console.log("getNoticeList",r.data);
          let l = r.data.list || [];
          // v.setData({listMode:mode});
          v.setData({listMode:true,noticeList: l});
        }
      }
    })
  },
  getNoticeDetail(notice_id_){
    let v = this;
    let url = app.globalData.host + '/api/notice/detail';
    let token = app.globalData.token;
    let notice_id = notice_id_ || v.data.notice_id;
    wx.request({ url: url,data:{notice_id:notice_id,token: token},success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          v.setData({listMode: false, notice: r.data.title, notice_detail: r.data.text});
        }
      }
    })
  },
  navTap(ev){
    let v = this;
    let ds = ev.currentTarget.dataset;
    let ll = v.data.navs;
    if(ds && ll[ds.idx]) {
      let lk = ll[ds.idx];
      let path = '/';
      if(lk){
        if(lk.path) {
          path = lk.path ;
          wx.reLaunch({ url: path});
        }
      }
      console.log('navTap',path, lk);
    }
  },

  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },

  onShareAppMessage: function () {
  }
})